package com.example.accessibilitydemo;

import android.annotation.TargetApi;
import android.app.Notification.Action;
import android.app.PendingIntent;
import android.service.notification.StatusBarNotification;


/**
 * Created by Administrator on 2018/7/10.
 */

public class NotificationCall {

    private static final String ANSWER_KEYWORDS[] = {"Answer", "接听", "إجابة", "Antworten", "Répondre", "Responda", "उत्तर", "Responder", "Risposta", "Cevap", "Answer"};
    private static final String ENDCALL_KEYWORDS[] = {"Decline", "拒绝", "رفض", "Ablehnen", "Déclin", "Declínio", "पतन", "Declínio", "Declino", "düşüş","Decline"};
    private static final NotificationCall sNotificationCall = new NotificationCall();
    private PendingIntent mAcceptCallPendingIntent;
    private PendingIntent mEndCallPendingIntent;

    public static NotificationCall get() {
        return sNotificationCall;
    }

    public void setAcceptCallPendingIntent(PendingIntent paramPendingIntent) {
        mAcceptCallPendingIntent = paramPendingIntent;
    }

    public void setEndCallPendingIntent(PendingIntent paramPendingIntent) {
        mEndCallPendingIntent = paramPendingIntent;
    }

    @TargetApi(19)
    public void updateStatusBarNotification(StatusBarNotification paramStatusBarNotification) {
        try {
            if (paramStatusBarNotification.getNotification().actions != null) {
                Action[] actions = paramStatusBarNotification.getNotification().actions;
                int j = actions.length;
                int i = 0;
                while (i < j) {
                    Action action = actions[i];
                    for (String str : ANSWER_KEYWORDS) {
                        if (!action.title.toString().equalsIgnoreCase(str))
                            continue;
                        setAcceptCallPendingIntent(action.actionIntent);
                    }
                    for (String str : ENDCALL_KEYWORDS) {
                        if (!action.title.toString().equalsIgnoreCase(str))
                            continue;
                        setEndCallPendingIntent(action.actionIntent);
                    }
                    i += 1;
                }
            }
        } catch (Exception e) {
        }
    }

    public void sendAcceptCallPendingIntent() {
        try {
            if (mAcceptCallPendingIntent != null) {
                mAcceptCallPendingIntent.send();
            }
        } catch (PendingIntent.CanceledException e) {
        }
    }

    public void sendEndCallPendingIntent() {
        try {
            if (mEndCallPendingIntent != null) {
                mEndCallPendingIntent.send();
            }
        } catch (PendingIntent.CanceledException e) {
        }
    }
}
